package com.dmlll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.DataDictionaryVO;
import com.dmlll.common.DataGridView;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.DataDictionary;
import com.dmlll.service.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
public class DataDictionaryController {
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @RequestMapping("loadData")
    public DataGridView loadData(DataDictionaryVO dataDictionary){
        IPage<DataDictionary> page = new Page<>(dataDictionary.getPage(),dataDictionary.getLimit());
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq(dataDictionary.getDateType()!=null&&dataDictionary.getDateType()>0,"date_Type",dataDictionary.getDateType());
        IPage<DataDictionary> iPage = dataDictionaryService.page(page, wrapper);
        return new DataGridView(iPage.getTotal(),iPage.getRecords());
    }
    @RequestMapping("getDataDescription")
    public List<DataDictionary > getDataDescription(){
        QueryWrapper<DataDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("date_description");
        return dataDictionaryService.list(queryWrapper);
    }
    @RequestMapping("add")
    public ResultObj add(DataDictionary dataDictionary){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("date_description",dataDictionary.getDateDescription());
        wrapper.orderByDesc("date_type").last("limit 1");
        DataDictionary list = dataDictionaryService.getOne(wrapper);
        DataDictionary dictionary = dataDictionaryService.getOne(new QueryWrapper<DataDictionary>().orderByDesc("date_type").last("limit 1"));
        if(list==null){
            dataDictionary.setDataNum(1);
            dataDictionary.setDateType(dictionary.getDateType()+1);
        }else {
            dataDictionary.setDataNum(list.getDataNum()+1);
            dataDictionary.setDateType(list.getDateType());
        }
        try {
            dataDictionaryService.save(dataDictionary);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }
    @RequestMapping("update")
    public ResultObj update(DataDictionary dataDictionary){
        try {
            dataDictionaryService.updateById(dataDictionary);
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }
    @RequestMapping("delete")
    public ResultObj delete(Integer id){
        try {
            dataDictionaryService.removeById(id);
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }
    @RequestMapping("getDataDictionaryName")
    public String getDataDictionaryName(Integer dateType,Integer dataNum){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("data_num",dataNum)
                .eq("date_type",dateType);
        return dataDictionaryService.getOne(wrapper).getDataName();
    }
    /**
     * all
     * @return
     */
    @RequestMapping("getDataDictionary")
    public DataGridView getDataDictionary(Integer dateType){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("date_type",dateType);
        List<DataDictionary> list = dataDictionaryService.list(wrapper);
        return new DataGridView(list);
    }
    /**
     * 采购单状态
     * @return
     */
    @RequestMapping("getPurchaseState")
    public DataGridView getPurchaseState(){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("date_type",2);
        List<DataDictionary> list = dataDictionaryService.list(wrapper);
        return new DataGridView(list);
    }

    /**
     * 采购状态
     * @return
     */
    @RequestMapping("getPurchaseSta")
    public DataGridView getPurchaseSta(){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("date_type",3);
        List<DataDictionary> list = dataDictionaryService.list(wrapper);
        return new DataGridView(list);
    }
    @RequestMapping("getQuality")
    public DataGridView getQuality(){
        QueryWrapper<DataDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("date_type",7);
        List<DataDictionary> list = dataDictionaryService.list(wrapper);
        return new DataGridView(list);
    }
}

